package com.ml.mall.service.common.impl;

import com.ml.mall.entity.seq.SysSeq;
import com.ml.mall.mapper.common.CommonSeqMapper;
import com.ml.mall.service.common.CommonSeqService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

/**
 * Created by kyyang on 2021/1/17 17:06
 */
@Service
public class CommonSeqServiceImpl implements CommonSeqService {

    @Autowired
    private CommonSeqMapper commonSeqMapper;


    @Override
    public Long curSeq(String seqName) {
        SysSeq sysSeq = commonSeqMapper.curSysOrderSeq(seqName);
        if(sysSeq==null){
            return -999L;
        }
        return sysSeq.getCurrentVal();
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public synchronized Long nextSeq(String seqName) {
        commonSeqMapper.updateSysOrderSeq(seqName);
        return curSeq(seqName);
    }

    @Override
    public Long curSysOrderSeq(String seqName) {
        SysSeq sysSeq = commonSeqMapper.curSysOrderSeq(seqName);
        if(sysSeq==null){
            return -999L;
        }
        StringBuilder sb = new StringBuilder();
        if(sysSeq.getPrefix()!=null){
            sb.append(sysSeq.getPrefix());
        }
        Random random = new Random();
//        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
        int rannum = (int) (random.nextDouble() * (999 - 100 + 1)) + 100;
        sb.append(rannum);
        sb.append(sysSeq.getCurrentVal());
        return Long.valueOf(sb.toString());
    }

    private Long curSysOrderSeq(String seqName,int num) {
        SysSeq sysSeq = commonSeqMapper.curSysOrderSeq(seqName);
        if(sysSeq==null){
            return -999L;
        }

        StringBuilder sb = new StringBuilder();
        if(sysSeq.getPrefix()!=null){
            sb.append(sysSeq.getPrefix());
        }
        if(num>0){
            Random random = new Random();
            Long r1 = Double.valueOf((Math.pow(10.0,Double.valueOf(num))-1)).longValue();
            Long r2 = Double.valueOf((Math.pow(10.0,Double.valueOf(num-1)))).longValue();
            Long rannum = (int) (random.nextDouble() * (r1 - r2 + 1)) + r2;
            sb.append(rannum);
        }
        sb.append(sysSeq.getCurrentVal());
        return Long.valueOf(sb.toString());
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public synchronized Long nextSysOrderSeq(String seqName) {
        commonSeqMapper.updateSysOrderSeq(seqName);
        return curSysOrderSeq(seqName);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public synchronized Long nextSysOrderSeq(String seqName, int num) {
        commonSeqMapper.updateSysOrderSeq(seqName);
        return curSysOrderSeq(seqName,num);
    }

}
